package main;

import java.io.BufferedReader;
import java.io.FileReader;

import org.apache.uima.UIMAFramework;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.ResourceSpecifier;
import org.apache.uima.util.XMLInputSource;

import com.giw.uima.ts.CoachAnnotation;
import com.giw.uima.ts.CompetitionAnnotation;
import com.giw.uima.ts.PersonAnnotation;
import com.giw.uima.ts.PlayerAnnotation;
import com.giw.uima.ts.TeamAnnotation;

public class Experiment {
	private final static String DOC_PATH="src/main/resources/dictionaries/filefinale.txt";


	public static void main(String[] args) throws ResourceInitializationException{
		Experiment exp=new Experiment();
		exp.runExperiments();
	}

	private String getNextDocument(String path,int i){
		String line=null;
		try{
			FileReader f = new FileReader(path);; 
			BufferedReader in = new BufferedReader(f);  
			line=in.readLine();
			int j=0;
			while(j<i){
				line=in.readLine();
				j++;
			}
			f.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return line;
	}

	private void runAnalysisEngineDescriptor(String text,JCas jcas,
			AnalysisEngine engine) throws AnalysisEngineProcessException, ResourceInitializationException {
		try {

			// set the text to be analyzed inside the JCas
			jcas.setDocumentText(text);

			// force document language to "x-unspecified" (usually this is extracted)
			jcas.setDocumentLanguage("x-unspecified");

			// run the analysis engine on the JCas
			engine.process(jcas);

		} catch (Exception e) {
			throw new AnalysisEngineProcessException(e);
		}
	}


	public void runExperiments() throws ResourceInitializationException{
		int i=0;
		String text;
		while((text=this.getNextDocument(DOC_PATH, i))!=null){
			this.runUima(text);
			i++;
		}
	}
	
	private void runUima(String text) throws ResourceInitializationException{
		AnalysisEngine engine=null;
		try{
			// create a UIMA XML resource
			XMLInputSource in = new XMLInputSource("src/main/resources/AggregateGiw1011.xml");

			// parse the XML to access the resource defined properties
			ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);

			// create Analysis Engine from the ResourceSpecifier
			engine = UIMAFramework.produceAnalysisEngine(specifier);
			
			int totalAnnotation=0;
			JCas jcas=engine.newJCas();
			System.out.println("-----------------DOCUMENTO:"+text.substring(0,9)+"-----------------------");
			System.out.println("---------------------------------------------");
			this.runAnalysisEngineDescriptor(text, jcas, engine);
			System.out.println("-----------------PERSONE---------------------");
			for(Annotation annotation:jcas.getAnnotationIndex(PersonAnnotation.type)){
				System.out.println(annotation.getCoveredText());
				totalAnnotation++;
			}
			System.out.println();
			System.out.println("-----------------TEAM---------------------");
			for(Annotation annotation:jcas.getAnnotationIndex(TeamAnnotation.type)){
				System.out.println(annotation.getCoveredText());
				totalAnnotation++;
			}
			System.out.println();
			System.out.println("-----------------COMPETIZIONI---------------------");
			for(Annotation annotation:jcas.getAnnotationIndex(CompetitionAnnotation.type)){
				System.out.println(annotation.getCoveredText());
				totalAnnotation++;
			}
			System.out.println();
			System.out.println("-----------------CALCIATORI---------------------");
			for(Annotation annotation:jcas.getAnnotationIndex(PlayerAnnotation.type)){
				System.out.println(annotation.getCoveredText());
				totalAnnotation++;
			}
			System.out.println();
			System.out.println("-----------------COACH---------------------");
			for(Annotation annotation:jcas.getAnnotationIndex(CoachAnnotation.type)){
				System.out.println(annotation.getCoveredText());
				totalAnnotation++;
			}
			System.out.println();
			System.out.println("-----------------TOTAL ANNOTATION:"+totalAnnotation+"-----------------");
			System.out.println();
			System.out.println();
			jcas=null;
			Runtime.getRuntime().gc();
			
		}
		catch(Exception e){
			throw new ResourceInitializationException(e);
		}
		
	}

}
